﻿<div class="tt-attach">
    <ul class="op">
        <li>
            @Html.ActionLink("返回分类列表", "List", null, new { onclick = "Tree.Back(this); return false;", @class = "btn btn-dirleft" })
        </li>
    </ul>
</div>
<div class="col-main">
    <div class="main-wrap box-sub" id="treePart">
        <ul id="tree">
        </ul>
    </div>
</div>
<div class="col-extra">
    <div class="box-sub hidden" id="infoPart">
        <h5 class="btn-userins">
            当前节点信息</h5>
        <div class="tip-alert-box ">
            <table class="tbl-info ">
                <tbody>
                    <tr>
                        <th>
                            唯一标识：
                        </th>
                        <td id="paramID">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            参数名称：
                        </th>
                        <td id="paramName">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            参数代码：
                        </th>
                        <td id="paramCode">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            参数标识：
                        </th>
                        <td id="paramCodeID">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            标识路径：
                        </th>
                        <td id="paramIDPath">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            名称路径：
                        </th>
                        <td id="paramNamePath">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            层级：
                        </th>
                        <td id="paramLevel">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            参数备注：
                        </th>
                        <td id="paramRemark">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            状态：
                        </th>
                        <td id="paramStatus">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" style="text-align: center; padding:5px;">
                            @Html.ActionLink("添加子节点", "CreateParam", new { id = ViewBag.TypeID }, new { id = "btnInsert", onclick = "Tree.Create(this);return false;", @class = "btn-add" })
                            @Html.ActionLink("修改当前节点", "EditParam", new { id = ViewBag.TypeID }, new { id = "btnEdit", onclick = "Tree.Edit(this);return false;", @class = "btn-edit" })
                            @Html.ActionLink("删除当前节点", "DeleteParam", new { id = ViewBag.TypeID }, new { id = "btnRemove", onclick = "Tree.Delete(this);return false;", @class = "btn-del" })
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    <div class="box-sub">
        <h5 id="btip" class="btn-tip">
            提示</h5>
        <ul class="s001 m">
            <li>当选中的分类包含子分类时，不能删除选中分类。</li>
        </ul>
    </div>
</div>
@Html.Hidden("treeDataUrl", Url.Action("TreeData", new { id = ViewBag.TypeID }))
@Html.Hidden("MoveUrl", Url.Action("MoveParam", new { id = ViewBag.TypeID }))
@Html.Hidden("paramDataUrl", Url.Action("ParamData", new { id = ViewBag.TypeID }))
<script type="text/javascript">

    $(function () {
        var treeE = $(".tabs-panels .panel:visible").find("#tree");
        treeE.tree({

            dnd: true,
            url: $(".tabs-panels .panel:visible").find("#treeDataUrl").val() + "?r=" + Math.random(),
            onClick: function (node) {
                //切换节点展开状态
                $(this).tree('expand', node.target);

                //呈现节点信息
                Tree.Show(node);
            },
            onDrop: function (target, source, point) {
                var target = $('#tree').tree("getNode", target);
                if (target.id == 0 && point != "append") {
                    $('#tree').tree("reload");
                    alert("不可移动到顶级节点同级！");
                    return false;
                }
                Tree.Move(source, target, point);
                //alert($(source).html() + "-" + source.id + point);
            }
        });

        //设置树宽度及滚动条
        treeE.height($(document).height() - 160 - $(".pg-footer").height() - 70 - 40);
        treeE.css("overflow-y", "auto");
    });

    var Tree = {};
    Tree.Back = function (e) {
        Common.CloseTab();
    }

    Tree.Show = function (node) {
        $.getJSON($(".tabs-panels .panel:visible").find("#paramDataUrl").val(), { complexParameterID: node.id }, function (data) {
            $(".tabs-panels .panel:visible").find("#paramName").text(data.ComplexParameterName);
            $(".tabs-panels .panel:visible").find("#paramCode").text(data.ComplexParameterCode);
            $(".tabs-panels .panel:visible").find("#paramCodeID").text(data.ComplexParameterCodeID);
            $(".tabs-panels .panel:visible").find("#paramID").text(data.UID);
            $(".tabs-panels .panel:visible").find("#paramIDPath").text(data.IDPath);
            $(".tabs-panels .panel:visible").find("#paramNamePath").text(data.NamePath);
            $(".tabs-panels .panel:visible").find("#paramLevel").text(data.ComplexParameterLevel);
            $(".tabs-panels .panel:visible").find("#paramRemark").text(data.Remark);
            $(".tabs-panels .panel:visible").find("#paramStatus").text(data.StateCodeID == 1 ? "启用" : "禁用");
            $(".tabs-panels .panel:visible").find("#infoPart").show();

            //判断是否有子节点
            if (data.HasChild) $(".tabs-panels .panel:visible").find("#btnRemove").hide();
            else $(".tabs-panels .panel:visible").find("#btnRemove").show();

        });
    }
    Tree.Create = function (e) {
        var selected = $(".tabs-panels .panel:visible").find('#tree').tree("getSelected");
        if (selected == null) {
            alert("没有找到选中节点，请先选中一个节点！");
            return;
        }

        $.createWin({ url: e.href + "?complexParameterID=" + selected.id, width: 600, height: 350, title: '新增复合参数' });
    }
    Tree.Edit = function (e) {
        var selected = $(".tabs-panels .panel:visible").find('#tree').tree("getSelected");
        if (selected == null) {
            alert("没有找到选中节点，请先选中需要编辑的节点！");
            return;
        }

        $.createWin({ url: e.href + "?complexParameterID=" + selected.id, width: 600, height: 350, title: '修改复合参数' });
    }
    Tree.Delete = function (e) {
        if (!confirm("确认要删除吗？"))
            return;

        var selected = $(".tabs-panels .panel:visible").find('#tree').tree('getSelected');
        if (selected == null) {
            alert("请选中要删除的节点。");
            return;
        }

        $.post(e.href + "?complexParameterID=" + selected.id, {}, function (data) {
            if (data.IsOK) {
                var parent = $(".tabs-panels .panel:visible").find('#tree').tree('getParent', selected.target);
                $(".tabs-panels .panel:visible").find('#tree').tree('remove', selected.target);
                $(".tabs-panels .panel:visible").find('#tree').tree('select', parent.target);
                Tree.Show(parent);
            }
            else {
                alert(data.Description);
            }
        });
    }
    Tree.Move = function (source, target, point) {
        var url = $(".tabs-panels .panel:visible").find("#MoveUrl").val();
        var pointID = 0;
        if (point == "append") pointID = 1;
        else if (point == "top") pointID = 2;
        else if (point == "bottom") pointID = 3;
        var postData = { movedID: source.id, targetID: target.id, moveType: pointID };
        $.post(url, postData, function (data) {
            if (data.IsOK) {
                alert("节点移动成功");
            }
            else {
                alert(data.Description);
                $(".tabs-panels .panel:visible").find('#tree').tree('reload');
            }
        });
    }
</script>
